package com.bnlr.lrparking.appui.stall;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.constant.LrConstant;
import com.bumptech.glide.Glide;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.adapter.MaterriaDevelopersAdapter;
import com.bnlr.lrparking.adapter.StallDealAdapter;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.model.DevelopersM;
import com.bnlr.lrparking.model.SelectDevelopersM;
import com.bnlr.lrparking.model.StallListBean;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.view.CarCommon;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * 开发商资料
 *
 * @author hp
 * @date 2017/10/14
 */

public class DevelopersMaterialActivity extends BaseActivity {

    private RelativeLayout re_post_list, re_aptitude, re_aptitude_img;
    private TextView tv_post_num, tv_post_name, tv_aptitude_name, tv_aptitude;
    private View post_line, aptitude_line;
    private RecyclerView recy_post, recy_knockdown;
    private MaterriaDevelopersAdapter selectDevelopersAdapter;
    private StallDealAdapter adapter;
    private List<StallListBean.DataBean> list = new ArrayList<>();
    private List<StallListBean.DataBean> listFinish = new ArrayList<>();
    private ImageView developers_back;
    private TextView title_developer_name, developer_name, developer_time, developer_num1,
            developer_num2, developer_num3, developer_num4;//开发商名称，入驻时间，已售，待售，已租，待租
    private ImageView develover_logo;
    private RelativeLayout developer_phone;
    private CarCommon carCommon = new CarCommon();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_developersmaterial);
        initView();
        getBargainData();

    }

    private void initView() {
        //getDealData("1",11,1);
        title_developer_name = (TextView) findViewById(R.id.title_developer_name);
        developer_name = (TextView) findViewById(R.id.developer_name);
        developer_time = (TextView) findViewById(R.id.developer_time);
        developer_num1 = (TextView) findViewById(R.id.developer_num1);
        developer_num2 = (TextView) findViewById(R.id.developer_num2);
        developer_num3 = (TextView) findViewById(R.id.developer_num3);
        developer_num4 = (TextView) findViewById(R.id.developer_num4);
        tv_aptitude = (TextView) findViewById(R.id.tv_aptitude);
        developer_phone = (RelativeLayout) findViewById(R.id.developer_phone);
        getDevelopersMessage(getIntent().getStringExtra("developId"));
        getDeveloperList();
        re_post_list = (RelativeLayout) findViewById(R.id.re_post_list);
        re_aptitude = (RelativeLayout) findViewById(R.id.re_aptitude);
        re_post_list.setOnClickListener(this);
        re_aptitude.setOnClickListener(this);
        tv_post_num = (TextView) findViewById(R.id.tv_post_num);
        tv_post_name = (TextView) findViewById(R.id.tv_post_name);
        post_line = (View) findViewById(R.id.post_line);
        tv_aptitude_name = (TextView) findViewById(R.id.tv_aptitude_name);
        aptitude_line = (View) findViewById(R.id.aptitude_line);
        re_aptitude_img = (RelativeLayout) findViewById(R.id.re_aptitude_img);
        recy_post = (RecyclerView) findViewById(R.id.recy_post);//成交
        recy_knockdown = (RecyclerView) findViewById(R.id.recy_knockdown);//楼盘
        developers_back = (ImageView) findViewById(R.id.developers_back);
        develover_logo = (ImageView) findViewById(R.id.develover_logo);
        developers_back.setOnClickListener(this);
        developer_phone.setOnClickListener(this);
        recy_post.setNestedScrollingEnabled(false);
        recy_post.setLayoutManager(new LinearLayoutManager(this));
        recy_knockdown.setLayoutManager(new LinearLayoutManager(this));
        adapter = new StallDealAdapter(listFinish);
        recy_knockdown.setAdapter(adapter);
        //adapter.notifyDataSetChanged();

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.re_post_list:
                setPostList();
                break;
            case R.id.re_aptitude:
                setAptitude();
                break;
            case R.id.developers_back:
                finish();
                break;
            case R.id.developer_phone:
                carCommon.phoneDialog(DevelopersMaterialActivity.this);
                break;
        }
    }

    //帖子
    private void setPostList() {
        tv_post_num.setTextColor(getResources().getColor(R.color.colorAccent));
        tv_post_name.setTextColor(getResources().getColor(R.color.colorAccent));
        post_line.setVisibility(View.VISIBLE);
        tv_aptitude_name.setTextColor(getResources().getColor(R.color.gray03_color));
        tv_aptitude.setTextColor(getResources().getColor(R.color.gray03_color));
        aptitude_line.setVisibility(View.GONE);
        re_aptitude_img.setVisibility(View.GONE);
        recy_post.setVisibility(View.VISIBLE);
    }

    //成交
    private void setAptitude() {
        tv_post_num.setTextColor(getResources().getColor(R.color.gray03_color));
        tv_post_name.setTextColor(getResources().getColor(R.color.gray03_color));
        post_line.setVisibility(View.GONE);
        tv_aptitude_name.setTextColor(getResources().getColor(R.color.colorAccent));
        tv_aptitude.setTextColor(getResources().getColor(R.color.colorAccent));
        aptitude_line.setVisibility(View.VISIBLE);
        re_aptitude_img.setVisibility(View.VISIBLE);
        recy_post.setVisibility(View.GONE);
    }


    //获取楼盘列表
    private void getDeveloperList() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "GetInSaleParking");
            mRequest.add("parkId", "0");
            mRequest.add("cityName", AppConfig.getInstance().getString("strCity", ""));
            mRequest.add("keyword", "");
            mRequest.add("pageindex", "1");
            mRequest.add("developerId", getIntent().getStringExtra("developId"));
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<SelectDevelopersM>(baseContext, true, SelectDevelopersM.class) {
                        @Override
                        public void doWork(final SelectDevelopersM data, String code) {
                            if (code.equals("1")) {

                                tv_post_num.setText(data.getRecordCount());
                                tv_aptitude.setText(data.getData().get(0).getSoldCount() + "");
                                selectDevelopersAdapter = new MaterriaDevelopersAdapter(data.getData());
                                recy_post.setAdapter(selectDevelopersAdapter);
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if (!code.equals("1")) {
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {
        }
    }

    private void getDevelopersMessage(final String developerId) {

        try {
            mRequest = NoHttp.createStringRequest(Urls.Developer, LrConstant.POST);
            mRequest.add("action", "GetDeveloperList");
            mRequest.add("cityName", AppConfig.getInstance().getString("strCity", ""));
            mRequest.add("keyword", "");
            mRequest.add("pageindex", "1");
            mRequest.add("developerId", getIntent().getStringExtra("developId"));

            // 添加到请求队列+
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomHttpListener<DevelopersM>(this, true, DevelopersM.class) {
                        @Override
                        public void doWork(DevelopersM data, String code) {
                            if (TextUtils.equals("1", code)) {
                                title_developer_name.setText(data.getData().get(0).getName());
                                developer_name.setText(data.getData().get(0).getName());

                                String date = data.getData().get(0).getBeginTime().split(" ")[0];
                                developer_time.setText(date.split("-")[0] + "年" + date.split("-")[1] + "月" + date.split("-")[2] + "日注册");
                                developer_num1.setText(data.getData().get(0).getTotalCount() - data.getData().get(0).getSoldCount() + "");
                                developer_num2.setText(data.getData().get(0).getSoldCount() + "");
                                Glide.with(DevelopersMaterialActivity.this)
                                        .load(data.getData().get(0).getPhoto())
                                        .bitmapTransform(new CropCircleTransformation(DevelopersMaterialActivity.this))
                                        .into(develover_logo);
                                developer_num3.setText(data.getData().get(0).getRentedCount() + "");
                                developer_num4.setText(data.getData().get(0).getRentingCount() + "");
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if ("1".equals(code)) {
                                } else {
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, false);
        } catch (Exception e) {
        }
    }

    //车位列表

    /**
     * 获取列表数据
     */
    private void getDealData(String spaceowertype, int type, final int page) {
        /**
         * spaceowertype:0是个人1开发商
         * areaId:地区id
         * type：类型
         * keys：关键字
         * orderBy:排序方式
         * SpaceType：车位类型
         */
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP_SHOW, LrConstant.POST);
            mRequest.add("action", "GetShowInfoTable");
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("pageIndex", page);
            mRequest.add("spaceOwnerType", spaceowertype);//0是个人1是开发商
            mRequest.add("parkingId", getIntent().getStringExtra("developId"));
            mRequest.add("cityName", AppConfig.getInstance().getString("strCity", ""));
            mRequest.add("type", type);
            // 添加到请求队列+
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomHttpListener<StallListBean>(this, true, StallListBean.class) {
                        @Override
                        public void doWork(StallListBean data, String code) {
                            if (TextUtils.equals("1", code)) {
                                if (adapter != null) {
                                    if (data != null && data.getData().size() != 0) {
                                        if (page == 1) {
                                            list = data.getData();
                                            adapter.setNewData(data.getData());
                                        } else {
                                            list.addAll(data.getData());
                                            adapter.notifyDataSetChanged();
                                        }
                                        adapter.loadMoreComplete();
                                    } else {
                                        if (page == 1) {
                                            list.clear();
                                            adapter.notifyDataSetChanged();
                                        } else {
                                            adapter.loadMoreEnd();
                                        }
                                    }
                                }
                            } else {
                                ToastUtils.showShort(data.getMsg() + "");
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if ("1".equals(code)) {
                                } else {

                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {
        }
    }


    /**
     * 获取成交数据列表
     */
    private void getBargainData() {
        /**
         * spaceowertype:0是个人1开发商
         * areaId:地区id
         * type：类型
         * keys：关键字
         * orderBy:排序方式
         * SpaceType：车位类型
         */
        try {
            mRequest = NoHttp.createStringRequest(Urls.Developer, LrConstant.POST);
            mRequest.add("action", "GetDevelopSpace");
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("pageIndex", 1);
            mRequest.add("Status", "3");//0是个人1是开发商
            mRequest.add("developId", getIntent().getStringExtra("developId"));
            // 添加到请求队列+
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomHttpListener<StallListBean>(this, true, StallListBean.class) {
                        @Override
                        public void doWork(StallListBean data, String code) {
                            LogUtils.e("1111+++++++++");
                            if (TextUtils.equals("1", code)) {
                                if (adapter != null) {
                                    if (data != null && data.getData().size() != 0) {
                                        LogUtils.e("22222+++++++++");
                                        listFinish.clear();
                                        listFinish = data.getData();
                                        adapter.setNewData(listFinish);
                                    } else {
                                        listFinish.clear();
                                        adapter.notifyDataSetChanged();
                                    }
                                }
                            } else {
                                ToastUtils.showShort(data.getMsg() + "");
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if ("1".equals(code)) {
                                } else {

                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {
        }
    }
}
